/*   1:    */ package com.jetbrains.licenseService.util;
/*   2:    */ 
/*   3:    */

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.StringReader;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;

/*   4:    */
/*   5:    */
/*   6:    */
/*   7:    */
/*   8:    */
/*   9:    */
/*  10:    */
/*  11:    */
/*  12:    */
/*  13:    */
/*  14:    */
/*  15:    */
/*  16:    */
/*  17:    */
/*  18:    */
/*  19:    */
/*  20:    */
/*  21:    */
/*  22:    */ 
/*  23:    */ public class ResponseMarshaller
/*  24:    */ {
/*  25:    */   private static final String a = "com.jetbrains.licenseService.responses";
/*  26: 28 */   private static final ObjectPool<DocumentBuilder> b = new ObjectPool(1, new ObjectPool.Factory()
/*  27:    */   {
/*  28:    */     public DocumentBuilder create()
/*  29:    */     {
/*  30: 30 */       return ResponseMarshaller.access$000();
/*  31:    */     }
/*  32:    */     
/*  33:    */     public void onAllocate(DocumentBuilder paramAnonymousDocumentBuilder) {}
/*  34:    */     
/*  35:    */     public void onRelease(DocumentBuilder paramAnonymousDocumentBuilder) {}
/*  36: 28 */   });
/*  37:    */   
/*  38:    */   public static <T> T unmarshall(String paramString)
/*  39:    */   {
/*  40:    */     try
/*  41:    */     {
/*  42: 43 */       ObjectPool.Item localItem = b.allocate();
/*  43:    */       Document localDocument;
/*  44:    */       try
/*  45:    */       {
/*  46: 45 */         localDocument = ((DocumentBuilder)localItem.get()).parse(new InputSource(new StringReader(paramString)));
/*  47:    */       }
/*  48:    */       finally
/*  49:    */       {
/*  50: 48 */         localItem.release();
/*  51:    */       }
/*  52: 51 */       Element localElement = localDocument.getDocumentElement();
/*  53: 52 */       return a(localElement);
/*  54:    */     }
/*  55:    */     catch (Exception localException)
/*  56:    */     {
/*  57: 55 */       throw new RuntimeException(localException);
/*  58:    */     }
/*  59:    */   }
/*  60:    */   
/*  61:    */   private static Object a(Element paramElement)
/*  62:    */     throws ClassNotFoundException, InstantiationException, IllegalAccessException, IntrospectionException, InvocationTargetException
/*  63:    */   {
/*  64: 60 */     String str = b(paramElement.getNodeName());
/*  65: 61 */     Class localClass = Class.forName(str);
/*  66: 62 */     Object localObject1 = localClass.newInstance();
/*  67: 64 */     for (PropertyDescriptor localPropertyDescriptor : Introspector.getBeanInfo(localClass).getPropertyDescriptors())
/*  68:    */     {
/*  69: 65 */       NodeList localNodeList = paramElement.getElementsByTagName(localPropertyDescriptor.getName());
/*  70: 66 */       if (localNodeList.getLength() != 0)
/*  71:    */       {
/*  72: 67 */         Node localNode1 = localNodeList.item(0);
/*  73:    */         Object localObject3;
/*  74:    */         Object localObject2;
/*  75: 69 */         if (a(localPropertyDescriptor))
/*  76:    */         {
/*  77: 70 */           localObject3 = new ArrayList();
/*  78: 71 */           for (Node localNode2 = localNode1.getFirstChild(); localNode2 != null; localNode2 = localNode2.getNextSibling()) {
/*  79: 72 */             if ((localNode2 instanceof Element))
/*  80:    */             {
/*  81: 73 */               Object localObject4 = a((Element)localNode2);
/*  82: 74 */               ((Collection)localObject3).add(localObject4);
/*  83:    */             }
/*  84:    */           }
/*  85: 77 */           localObject2 = localObject3;
/*  86:    */         }
/*  87:    */         else
/*  88:    */         {
/*  89: 80 */           localObject3 = unescapeXml(localNode1.getTextContent());
/*  90: 81 */           localObject2 = convertValue((String)localObject3, localPropertyDescriptor.getPropertyType());
/*  91:    */         }
/*  92: 83 */         localPropertyDescriptor.getWriteMethod().invoke(localObject1, new Object[] { localObject2 });
/*  93:    */       }
/*  94:    */     }
/*  95: 87 */     return localObject1;
/*  96:    */   }
/*  97:    */   
/*  98:    */   public static <T> String marshall(T paramT)
/*  99:    */   {
/* 100: 91 */     return a(paramT);
/* 101:    */   }
/* 102:    */   
/* 103:    */   private static String a(Object paramObject)
/* 104:    */   {
/* 105:    */     try
/* 106:    */     {
/* 107: 96 */       StringBuilder localStringBuilder = new StringBuilder();
/* 108: 97 */       Class localClass = paramObject.getClass();
/* 109:    */       
/* 110: 99 */       String str = a(localClass.getName());
/* 111:100 */       localStringBuilder.append("<").append(str).append(">");
/* 112:101 */       for (PropertyDescriptor localPropertyDescriptor : Introspector.getBeanInfo(localClass).getPropertyDescriptors())
/* 113:    */       {
/* 114:102 */         Object localObject1 = localPropertyDescriptor.getReadMethod().invoke(paramObject, new Object[0]);
/* 115:103 */         if ((localObject1 != null) && (!(localObject1 instanceof Class)))
/* 116:    */         {
/* 117:104 */           localStringBuilder.append("<").append(localPropertyDescriptor.getName()).append(">");
/* 118:    */           Object localObject2;
/* 119:105 */           if (a(localPropertyDescriptor))
/* 120:    */           {
/* 121:106 */             localObject2 = (Collection)localObject1;
/* 122:107 */             for (Object localObject3 : (Collection)localObject2) {
/* 123:108 */               localStringBuilder.append(a(localObject3));
/* 124:    */             }
/* 125:    */           }
/* 126:    */           else
/* 127:    */           {
/* 128:112 */             localObject2 = Date.class.isAssignableFrom(localPropertyDescriptor.getPropertyType()) ? ClientUtil.formatDate((Date)localObject1) : localObject1.toString();
/* 129:113 */             localStringBuilder.append(c((String)localObject2));
/* 130:    */           }
/* 131:115 */           localStringBuilder.append("</").append(localPropertyDescriptor.getName()).append(">");
/* 132:    */         }
/* 133:    */       }
/* 134:118 */       localStringBuilder.append("</").append(str).append(">");
/* 135:    */       
/* 136:120 */       return localStringBuilder.toString();
/* 137:    */     }
/* 138:    */     catch (Exception localException)
/* 139:    */     {
/* 140:123 */       throw new RuntimeException(localException);
/* 141:    */     }
/* 142:    */   }
/* 143:    */   
/* 144:    */   private static boolean a(PropertyDescriptor paramPropertyDescriptor)
/* 145:    */   {
/* 146:128 */     return Collection.class.isAssignableFrom(paramPropertyDescriptor.getPropertyType());
/* 147:    */   }
/* 148:    */   
/* 149:    */   private static String a(String paramString)
/* 150:    */   {
/* 151:132 */     return paramString.substring(paramString.lastIndexOf(".") + 1);
/* 152:    */   }
/* 153:    */   
/* 154:    */   private static String b(String paramString)
/* 155:    */   {
/* 156:136 */     return "com.jetbrains.licenseService.responses." + paramString;
/* 157:    */   }
/* 158:    */   
/* 159:    */   public static Object convertValue(String paramString, Class paramClass)
/* 160:    */   {
/* 161:140 */     if (String.class.equals(paramClass)) {
/* 162:141 */       return paramString;
/* 163:    */     }
/* 164:143 */     if (paramClass.isEnum()) {
/* 165:144 */       return Enum.valueOf(paramClass, paramString);
/* 166:    */     }
/* 167:146 */     if ((Long.TYPE.equals(paramClass)) || (Long.class.equals(paramClass))) {
/* 168:147 */       return Long.valueOf(Long.parseLong(paramString));
/* 169:    */     }
/* 170:149 */     if ((Integer.TYPE.equals(paramClass)) || (Integer.class.equals(paramClass))) {
/* 171:150 */       return Integer.valueOf(Integer.parseInt(paramString));
/* 172:    */     }
/* 173:152 */     if ((Short.TYPE.equals(paramClass)) || (Short.class.equals(paramClass))) {
/* 174:153 */       return Short.valueOf(Short.parseShort(paramString));
/* 175:    */     }
/* 176:155 */     if ((Float.TYPE.equals(paramClass)) || (Float.class.equals(paramClass))) {
/* 177:156 */       return Float.valueOf(Float.parseFloat(paramString));
/* 178:    */     }
/* 179:158 */     if ((Double.TYPE.equals(paramClass)) || (Double.class.equals(paramClass))) {
/* 180:159 */       return Double.valueOf(Double.parseDouble(paramString));
/* 181:    */     }
/* 182:161 */     if ((Byte.TYPE.equals(paramClass)) || (Byte.class.equals(paramClass))) {
/* 183:162 */       return Byte.valueOf(Byte.parseByte(paramString));
/* 184:    */     }
/* 185:164 */     if ((Boolean.TYPE.equals(paramClass)) || (Boolean.class.equals(paramClass))) {
/* 186:165 */       return Boolean.valueOf(Boolean.parseBoolean(paramString));
/* 187:    */     }
/* 188:167 */     if (Date.class.equals(paramClass)) {
/* 189:    */       try
/* 190:    */       {
/* 191:169 */         return ClientUtil.parseDate(paramString);
/* 192:    */       }
/* 193:    */       catch (ParseException localParseException)
/* 194:    */       {
/* 195:172 */         throw new RuntimeException("Expected Date value in format \"yyyy-MM-dd\", but was \"" + paramString + "\"");
/* 196:    */       }
/* 197:    */     }
/* 198:175 */     throw new RuntimeException("Unsupported type " + paramClass.getName() + " in property value: " + paramString);
/* 199:    */   }
/* 200:    */   
/* 201:    */   private static String c(String paramString)
/* 202:    */   {
/* 203:179 */     return paramString == null ? null : Entities.XML.escape(paramString);
/* 204:    */   }
/* 205:    */   
/* 206:    */   public static String unescapeXml(String paramString)
/* 207:    */   {
/* 208:183 */     return paramString == null ? null : Entities.XML.unescape(paramString);
/* 209:    */   }
/* 210:    */   
/* 211:    */   private static DocumentBuilder a()
/* 212:    */   {
/* 213:187 */     DocumentBuilderFactory localDocumentBuilderFactory = DocumentBuilderFactory.newInstance();
/* 214:188 */     localDocumentBuilderFactory.setIgnoringComments(true);
/* 215:189 */     localDocumentBuilderFactory.setIgnoringElementContentWhitespace(true);
/* 216:    */     try
/* 217:    */     {
/* 218:191 */       return localDocumentBuilderFactory.newDocumentBuilder();
/* 219:    */     }
/* 220:    */     catch (ParserConfigurationException localParserConfigurationException)
/* 221:    */     {
/* 222:194 */       throw new IllegalStateException(localParserConfigurationException);
/* 223:    */     }
/* 224:    */   }
/* 225:    */ }



/* Location:           E:\idea\

 * Qualified Name:     com.jetbrains.licenseService.util.ResponseMarshaller

 * JD-Core Version:    0.7.0.1

 */